//
//  WmcBaseTabbarVC.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/3/30.
//

import UIKit
import YPBedrock

open class WmcBaseTabBarVC: UITabBarController {
    // Lifecycle

    deinit {
        WmcLogManager.DebugLog(type: .delloc, log: "Tabbar被释放\(self)")
    }

    override open func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .white
        setTabBarAppearance()
        makeUI() // 先进行布局
        bindViewModel() // 反向数据流绑定
        bindViewAction() // 正向数据流绑定
    }

    override open func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }

    override open func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
    }

    // Open

    open func setTabBarAppearance() {
        let itemAppearance = UITabBarItem.appearance()
        itemAppearance.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.wmc_mainTheme], for: .selected)
        itemAppearance.setTitleTextAttributes([NSAttributedString.Key.foregroundColor: UIColor.wmc_grayH14_CCCCCC], for: .normal)

        let tabbarAppearance = UITabBar.appearance()
        tabbarAppearance.isTranslucent = false

        if #available(iOS 15.0, *) {
            let appearance = UITabBarAppearance()
            appearance.backgroundColor = .white
            appearance.shadowColor = nil
            appearance.shadowImage = nil
            tabbarAppearance.scrollEdgeAppearance = appearance
            tabbarAppearance.standardAppearance = appearance
        }

        tabBar.barStyle = .black
        tabBar.barTintColor = .white
        tabBar.backgroundColor = .white
        tabBar.tintColor = UIColor.wmc_mainTheme
        tabBar.layer.shadowColor = UIColor.wmc_grayH15_E6E6E6.cgColor
        tabBar.layer.shadowOffset = CGSize(width: 0, height: -2)
        tabBar.layer.shadowRadius = 4
        tabBar.layer.shadowOpacity = 0.3
    }

    /// 添加子控制器
    open func addChildVC(_ childVC: UIViewController,
                         childTitle: String,
                         normalImage: UIImage,
                         selectedImage: UIImage,
                         rootPopVCBlock: YPBlock.Empty? = nil)
    {
        let navVC = WmcNavVC(rootViewController: childVC)
        navVC.rootPopVCBlock = rootPopVCBlock
        childVC.title = childTitle
        childVC.tabBarItem.tag = viewControllers?.count ?? 0
        childVC.tabBarItem.image = normalImage.withRenderingMode(.alwaysOriginal)
        childVC.tabBarItem.selectedImage = selectedImage.withRenderingMode(.alwaysTemplate)

        addChild(navVC)
    }

    /// 正向数据流（页面操作的监听）方法放在这里
    open func bindViewAction() {}

    /// 反向数据流（ViewModel的监听）进行数据绑定的放在这里
    open func bindViewModel() {}

    /// 创建UI的方法放在这里
    /// 最好按照，从上至下，从左至右依次添加视图
    open func makeUI() {}
}
